home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cpost_1_4.lha / cpostp2.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  11KB  |  337 lines

  1. /*------------------------------------------------------------------
  2.  * cpostp2.c : pass 2 of cPost
  3.  *------------------------------------------------------------------
  4.  * 12-02-91 originally by Patrick J. Mueller
  5.  * 12-03-92 converted from cBook to cPost
  6.  *------------------------------------------------------------------*/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #include "ctok.h"
  13. #include "cpost.h"
  14.  
  15. /*------------------------------------------------------------------
  16.  * static buffer
  17.  *------------------------------------------------------------------*/
  18. #define BUFFER_LEN  8000
  19. #define BRACKET_LEN  500
  20.  
  21. static unsigned char Buffer    [BUFFER_LEN];
  22. static unsigned char FuncBuff  [MAX_IDENT_LEN+1];
  23. static unsigned char CurrFunc  [MAX_IDENT_LEN+1];
  24. static unsigned char BrackInfo [BRACKET_LEN];
  25.  
  26. /*------------------------------------------------------------------
  27.  * global variables
  28.  *------------------------------------------------------------------*/
  29. static FILE           *oFile;
  30. static PageEject      *pageJ;
  31. static char           *Xchars[256];
  32. static unsigned char   currFont;
  33. static unsigned long   lineNo;
  34. static int             col;
  35.  
  36. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  37. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  38.  
  39. /*------------------------------------------------------------------
  40.  * write out the buffer, translating as we go
  41.  *------------------------------------------------------------------*/
  42. static void WriteOut(
  43.    unsigned char    *buffer,
  44.    int               len
  45.    )
  46.    {
  47.    int         i;
  48.    char       *xlate;
  49.  
  50.    /*---------------------------------------------------------------
  51.     * print each character in buffer
  52.     *---------------------------------------------------------------*/
  53.    for (i=0; i<len; i++)
  54.       {
  55.       xlate = Xchars[buffer[i]];
  56.  
  57.       /*------------------------------------------------------------
  58.        * check for end of line
  59.        *------------------------------------------------------------*/
  60.       if ('\n' == buffer[i])
  61.          {
  62.          lineNo++;
  63.  
  64.          /*---------------------------------------------------------
  65.           * print end of line info
  66.           *---------------------------------------------------------*/
  67.          fprintf(oFile,")] showLine %s\n",BrackInfo);
  68.          BrackInfo[0] = 0;
  69.  
  70.          /*---------------------------------------------------------
  71.           * see if there is a new function definition
  72.           *---------------------------------------------------------*/
  73.          if (*CurrFunc)
  74.             {
  75.             fprintf(oFile,"/currFunc (%s) def\n",CurrFunc);
  76.             *CurrFunc = 0;
  77.             }
  78.  
  79.          /*---------------------------------------------------------
  80.           * write page eject if we need to
  81.           *---------------------------------------------------------*/
  82.          if (info.oBreak && pageJ && (pageJ->lineNo == lineNo))
  83.             {
  84.             if (pageJ->next)
  85.                {
  86.                int lines;
  87.  
  88.                lines = pageJ->next->lineNo - pageJ->lineNo;
  89.                fprintf(oFile,"%d linesFit\n",lines);
  90.  
  91.                pageJ = pageJ->next;
  92.                }
  93.             }
  94.  
  95.          /*---------------------------------------------------------
  96.           * print start of line info
  97.           *---------------------------------------------------------*/
  98.          fprintf(oFile,"[%c (",currFont);
  99.  
  100.          col = 0;
  101.          }
  102.  
  103.       /*------------------------------------------------------------
  104.        * plain old character
  105.        *------------------------------------------------------------*/
  106.       else if (NULL == xlate)
  107.          {
  108.          col++;
  109.  
  110. /*       fwrite(&(buffer[i]),1,1,oFile); */
  111.          fputc(buffer[i],oFile);
  112.          }
  113.  
  114.       /*------------------------------------------------------------
  115.        * character to translate
  116.        *------------------------------------------------------------*/
  117.       else
  118.          {
  119.          char *bChar;
  120.          char  nBuff[20];
  121.  
  122.          col++;
  123.  
  124.          /*---------------------------------------------------------
  125.           * check for bracket character
  126.           *---------------------------------------------------------*/
  127.          bChar = NULL;
  128.          switch(buffer[i])
  129.             {
  130.             case '\x01' : bChar = "u"; break;
  131.             case '\x02' : bChar = "m"; break;
  132.             case '\x03' : bChar = "l"; break;
  133.             }
  134.  
  135.          if (bChar && (strlen(BrackInfo) < BRACKET_LEN - 30))
  136.             {
  137.             sprintf(nBuff,"%d",col);
  138.  
  139.  
  140.             strcat(BrackInfo," ");
  141.             strcat(BrackInfo,nBuff);
  142.             strcat(BrackInfo," ");
  143.             strcat(BrackInfo,bChar);
  144.             strcat(BrackInfo,"B");
  145.             }
  146.  
  147.          /*---------------------------------------------------------
  148.           * print translation
  149.           *---------------------------------------------------------*/
  150. /*       fwrite(xlate,1,strlen(xlate),oFile); */
  151.          fputs(xlate,oFile);
  152.          }
  153.       }
  154.    }
  155.  
  156. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  157. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  158.  
  159. /*------------------------------------------------------------------
  160.  * pass 2 - add formatting tags
  161.  *------------------------------------------------------------------*/
  162. int Pass2(
  163.    File     *file,
  164.    Info     *info
  165.    )
  166.    {
  167.    Tok           *next;
  168.    unsigned long  offs;
  169.    unsigned long  len;
  170.    FILE          *iFile;
  171.    int            i;
  172.  
  173.    /*---------------------------------------------------------------
  174.     * initialize some stuff
  175.     *---------------------------------------------------------------*/
  176.    oFile = info->oFile;
  177.  
  178.    for (i=0; i<256; i++)
  179.       {
  180.       Xchars[i] = NULL;
  181.       }
  182.  
  183.    Xchars['(']  = "\\(";    /* ( -> \( */
  184.    Xchars[')']  = "\\)";    /* ) -> \) */
  185.    Xchars['\\'] = "\\\\";   /* \ -> \\ */
  186.  
  187.    Xchars['\x01'] = " ";
  188.    Xchars['\x02'] = " ";
  189.    Xchars['\x03'] = " ";
  190.  
  191.    /*---------------------------------------------------------------
  192.     * print status and read file
  193.     *---------------------------------------------------------------*/
  194.    fprintf(stderr,"   Reading file %s\n",file->name);
  195.    cParse(file,info);
  196.  
  197.    /*---------------------------------------------------------------
  198.     * write header
  199.     *---------------------------------------------------------------*/
  200.    fprintf(info->oFile,"\n%%----------------------------------------------\n");
  201.  
  202.    if (file->date && file->time && *file->date && *file->time)
  203.       fprintf(info->oFile,"(%s) (%s   %s) startFile\n",file->name,
  204.                           file->date,file->time);
  205.    else
  206.       fprintf(info->oFile,"(%s) () startFile\n",file->name);
  207.  
  208.    /*---------------------------------------------------------------
  209.     * first line processing
  210.     *---------------------------------------------------------------*/
  211.    CurrFunc[0]  = 0;
  212.    BrackInfo[0] = 0;
  213.    fprintf(oFile,"[n (");
  214.  
  215.    /*---------------------------------------------------------------
  216.     * open the input file
  217.     *---------------------------------------------------------------*/
  218.    if (file->tempName)
  219.       iFile = fopen(file->tempName,"r");
  220.    else
  221.       iFile = fopen(file->pathName,"r");
  222.  
  223.    if (!iFile)
  224.       cPostError(1,"error opening file for reading");
  225.  
  226.    /*---------------------------------------------------------------
  227.     * initialize token processing
  228.     *---------------------------------------------------------------*/
  229.    next     = file->tokList;
  230.    pageJ    = file->breakList;
  231.    offs     = 0;
  232.    lineNo   = 0;
  233.    col      = 0;
  234.    currFont = 'n';
  235.  
  236.    /*---------------------------------------------------------------
  237.     * loop through tokens
  238.     *---------------------------------------------------------------*/
  239.    while (next)
  240.       {
  241.       /*---------------------------------------------------------
  242.        * read boring stuff up to first token
  243.        *---------------------------------------------------------*/
  244.       if (next->tok.offs != offs)
  245.          {
  246.          /*---------------------------------------------------------
  247.           * read and write it
  248.           *---------------------------------------------------------*/
  249.